package cn.fl.splrate.service.impl;

import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.splrate.service.IParseTempFileRecService;
import cn.fl.splrate.transform.ParseTempFileRecVOTransform;
import cn.fl.splrate.vo.ParseTempFileRecVO;
import com.alibaba.fastjson.JSON;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import fl.spl.facade.splrate.api.IParseTempFileRecApiService;
import fl.spl.facade.splrate.api.IParseTempFileRecQueryApiService;
import fl.spl.facade.splrate.condition.ParseTempFileRecQC;
import fl.spl.facade.splrate.dto.ParseTempFileRecDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @Description: 解析模板文件记录表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ParseTempFileRecServiceImpl implements IParseTempFileRecService {

    @Autowired
    private IParseTempFileRecQueryApiService parseTempFileRecQueryApiService;

    @Autowired
    private IParseTempFileRecApiService parseTempFileRecApiService;

    @Override
    public ParseTempFileRecVO selectById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "获取对象主键不能为空!");
        }
        //TODO 业务逻辑
        ParseTempFileRecDTO dto = null;//this.自定义ApiService.selectParseTempFileRecDTOById(id);
        return ParseTempFileRecVOTransform.toVO(dto);
    }

    @Override
    public Long add(ParseTempFileRecVO entity) throws BaseException {
        if (null == entity) {
            BaseLogUtils.newWebLogger("新增对象不存在").setLogOperType(LogOperEnum.REQ)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .warn(LogExceptionEnum.COMMON_CODE_0002);
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "新增对象不存在!");
        }
        StringBuilder rpcSign = new StringBuilder();
        rpcSign.append(System.currentTimeMillis()).append("_").append(CurrentThreadContext.getCurrentUserName()).append("_").append(CurrentThreadContext.getCurrentUserId());
        DataResultDTO dataResultDTO = this.parseTempFileRecApiService.addParseTempFileRecDTO(ParseTempFileRecVOTransform.toDTO(entity),SplSystemConstant.FL_WEB_CHL_CD,null,rpcSign.toString());
        Long id = null ;
        if (null != dataResultDTO && dataResultDTO.getSuccess() == BaseConstant.IS_YESNO_YES) {
            id = (Long)dataResultDTO.getData();
        }
        BaseLogUtils.newWebLogger("新增经销商评级导入文件信息").setKeys(JSON.toJSONString(dataResultDTO)).setTraceId(rpcSign.toString()).setClientIp(dataResultDTO.getServiceCd()).info();
        return id ;
    }

    @Override
    public boolean removeById(Long id) throws BaseException {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
        }
        ;
        //return this.parseTempFileRecApiService.removeParseTempFileRecDTOById(id);
        //TODO 业务逻辑
        return false;
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean removeByIds(List<Long> ids) throws BaseException {
        if (CollectionUtils.isEmpty(ids)) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "删除对象主键不能为空!");
        }
        ;
        return false;
    }

    @Override
    public boolean modify(ParseTempFileRecVO entity) throws BaseException {
        if (null == entity) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "修改对象不能为空!");
        }
        ;
        //return this.parseTempFileRecApiService.modifyParseTempFileRecDTO(ParseTempFileRecVOTransform.toDTO(entity));
        //TODO 业务逻辑
        return false;
    }

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page) {
            page = new Pager();
        }
        StringBuilder rpcSign = new StringBuilder();
        rpcSign.append(System.currentTimeMillis()).append("_").append(CurrentThreadContext.getCurrentUserName()).append("_").append(CurrentThreadContext.getCurrentUserId());
        DataResultDTO dataResultDTO = this.parseTempFileRecQueryApiService.searchParseTempFileRecDTOListPage(page, SplSystemConstant.FL_WEB_CHL_CD, null, rpcSign.toString());
        if (null != dataResultDTO && dataResultDTO.getSuccess() == BaseConstant.IS_YESNO_YES) {
            page = (Pager) dataResultDTO.getData();
        }
        BaseLogUtils.newWebLogger("分页查询经销商评级导入文件信息").setKeys(JSON.toJSONString(page)).setTraceId(rpcSign.toString()).setClientIp(dataResultDTO.getServiceCd()).info();
        return page;
    }

    /**
     * 根据条件查询
     *
     * @param parseTempFileRecQC
     * @return
     */
    @Override
    public List<ParseTempFileRecVO> searchParseTempFileRecVOList(ParseTempFileRecQC parseTempFileRecQC) throws BaseException {
        if (null == parseTempFileRecQC) {
            throw new BaseException(BaseExceptionConstant.COMMON_0001, "根据条件查询经销商评级导入文件信息条件为空！");
        }
        StringBuilder rpcSign = new StringBuilder();
        rpcSign.append(System.currentTimeMillis()).append("_").append(CurrentThreadContext.getCurrentUserName()).append("_").append(CurrentThreadContext.getCurrentUserId());
        DataResultDTO dataResultDTO = this.parseTempFileRecQueryApiService.searchParseTempFileRecDTOList(parseTempFileRecQC, SplSystemConstant.FL_WEB_CHL_CD, null, rpcSign.toString());
        List<ParseTempFileRecVO> parseTempFileRecVOList = null;
        if (null != dataResultDTO && dataResultDTO.getSuccess() == BaseConstant.IS_YESNO_YES) {
            parseTempFileRecVOList = ParseTempFileRecVOTransform.toVOList((List<ParseTempFileRecDTO>) dataResultDTO.getData());
        }
        BaseLogUtils.newWebLogger("根据条件查询经销商评级导入文件信息").setKeys(JSON.toJSONString(parseTempFileRecVOList)).setTraceId(rpcSign.toString()).setClientIp(dataResultDTO.getServiceCd()).info();
        return parseTempFileRecVOList;
    }
}
